// Example on page 21. my edition 2: use functions.
// Core idea: main() follow pseudo code, and other part instead with functions.

#include <stdio.h>

#define MAXLINE 10

char strnow[MAXLINE];
char strmax[MAXLINE];

void copystr(char source[], char dest[]);
int getstr(char s[], int lim);

int main()
{
    int c;
    int length, longest;
    int i, j, state;

    longest = length = 0;
    i = j = 0;

    while ((length = getstr(strnow, MAXLINE)) > 0)
    {
        if (longest < length)
            longest = length,
            copystr(strnow, strmax);
    }

    if (longest > 0)
        printf("\n\nLongest is:\n%d (MAYBE inlcude '\\n')\n", longest),
            printf("strmax is:\n%s\n\n", strmax);

    return 0;
}

int getstr(char s[], int lim)
{
    int c;

    int i = 0;

    while (i < (lim - 1) && (c = getchar()) != EOF && c != '\n')
    // '\n' means until the end of str.
    // lim-1, for safe. combine with c!='\n', leave locations for '\0' and '\n'.
    {
        strnow[i] = c,
        i++;
    }
    if (c == '\n')
        strnow[i] = '\n',
        i++;

    strnow[i] = '\0';

    return i;
}

void copystr(char source[], char dest[])
{
    char c;
    int i;

    i = 0;

    while ((c = source[i]) != '\0')
        dest[i] = source[i],
        i++;
    dest[i] = '\0';

    return;
}
